import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_track/base/base_logger.dart';
import 'package:flutter_track/flutter_track.dart';
import 'package:flutter_track/utils/file_save_utils.dart';
import 'package:flutter_track/utils/stack_trace_utils.dart';

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: '捣鼓的小玩意'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String str = "";
  String title = "";
  final String tag = "my_app";

  FlutterTrackManager? track;

  @override
  void initState() {
    super.initState();
    track = FlutterTrackManager();
    track?.init();
    title = "========> title init... <========";
  }

  @override
  Widget build(BuildContext context) {
    /// 进入开始track
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Row(
              children: [
                MaterialButton(
                  child: const Text("开始线程堆栈监听"),
                  onPressed: () {
                    title = "========> start trace listen... <========";
                    str = StackTraceUtils.transformStackTraceAnsSave(StackTrace.current.toString());
                    track?.doTrack(StackTraceUtils.baseIsolateInstructions, StackTraceUtils.baseVmInstructions);
                    setState(() {});
                  },
                ),
                MaterialButton(
                  child: const Text("模拟卡顿-读取文件"),
                  onPressed: () async {
                    title = "========> cartoooooon... <========";
                    sleep(const Duration(seconds: 3));
                    title = "========> cartoooooon end... <========";
                    setState(() {});

                    title = "=========> file read... <========";
                    Future.delayed(const Duration(seconds: 1))
                        .whenComplete(() async {
                      String content = await FileSaveUtils.readFile();
                      str = content;
                      setState(() {});
                    });
                  },
                ),
              ],
              mainAxisAlignment: MainAxisAlignment.spaceAround,
            ),
            Row(
              children: [
                MaterialButton(
                  child: const Text("模拟卡顿2"),
                  onPressed: () {
                    title = "========> cartooon2... start <========";
                    cartoon2();
                    title = "========> cartooon2... end <========";
                    setState(() {});
                  },
                ),
                MaterialButton(
                  child: const Text("模拟卡顿3"),
                  onPressed: () {
                    title = "========> cartoon3... start <========";
                    cartoon4();
                    title = "========> cartoon3... end <========";
                    setState(() {});
                  },
                ),
                MaterialButton(
                  child: const Text("模拟卡顿4"),
                  onPressed: () {
                    title = "========> cartoon4... start <========";
                    setState(() {});
                    var a = 0;
                    for (int i = 0; i < 1000000000000000; i++) {
                      a++;
                    }
                    Log.info(tag: tag, message: "a: $a");
                    title = "========> cartoon4... end <========";
                    setState(() {});
                  },
                ),
              ],
              mainAxisAlignment: MainAxisAlignment.spaceAround,
            ),
            Row(
              children: [
                MaterialButton(
                  child: const Text("文件读取"),
                  onPressed: () async {
                    title = "========> file read... <========";
                    str = await FileSaveUtils.readFile();
                    setState(() {});
                  },
                ),
                MaterialButton(
                  child: const Text("文件删除-读取"),
                  onPressed: () async {
                    title = "========> file delete and read... <========";
                    await FileSaveUtils.deleteAndCreate();
                    str = await FileSaveUtils.readFile();
                    setState(() {});
                  },
                ),
              ],
              mainAxisAlignment: MainAxisAlignment.spaceAround,
            ),
            Text(title),
            Container(
              padding: const EdgeInsets.only(
                  left: 5.0, right: 5.0, top: 2.0, bottom: 2.0),
              child: Column(
                children: [
                  Text(
                    str,
                    style: const TextStyle(
                      fontSize: 12,
                    ),
                    maxLines: null,
                    overflow: TextOverflow.visible,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  void cartoon4() {
    sleep(const Duration(seconds: 3));
  }

  void cartoon3() {
    sleep(const Duration(seconds: 3));
  }

  void cartoon2() {
    cartoon3();
  }
}
